package com.etooling.framework.repository;


import com.etooling.framework.entity.Org;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrgRepository extends BaseRepository<Org> {
    /**
     * 　　* @description:
     * 　　* @param
     * 　　* @return
     * 　　* @throws
     * 　　* @author 周志荣
     * 　　* @date 2020/12/18 23:42
     */
    @Query(nativeQuery = true, value = "WITH RECURSIVE a AS ( " +
            "SELECT id " +
            "  FROM framework.t_org " +
            "  WHERE id = ?1 " +
            "UNION ALL " +
            "  SELECT d.id " +
            "  FROM framework.t_org d " +
            "  JOIN a ON a.id = d.parent_id ) " +
            "SELECT * FROM a  ")
    public List<String> getOrgChildrenIdsByParentId(String parentOrgId);

    @Query(nativeQuery = true, value = "WITH RECURSIVE a AS ( " +
            "SELECT * " +
            "  FROM framework.t_org " +
            "  WHERE id = ?1 and company_id=?2 " +
            "UNION ALL " +
            "  SELECT d.* " +
            "  FROM framework.t_org d " +
            "  JOIN a ON a.id = d.parent_id ) " +
            "SELECT * FROM a   ")
    List<Org> getOrgChildrenByParentId(String rootId, String companyId);

    @Query(nativeQuery = true, value = "WITH RECURSIVE a AS ( " +
            "SELECT * " +
            "  FROM framework.t_org " +
            "  WHERE id = ?1 and company_id=?2 " +
            "UNION ALL " +
            "  SELECT d.* " +
            "  FROM framework.t_org d " +
            "  JOIN a ON a.id = d.parent_id ) " +
            "SELECT id FROM a   ")
    List<String> getOrgChildrenIdByParentId(String rootId, String companyId);

    @Query(nativeQuery = true,value = "select * from framework.t_org where auth_id=?1")
    Org getByAuthId(Integer auth_id);
}
